package com.sqoq.downloader3.base_ddg

import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.KeyEvent
import android.view.WindowManager
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.lifecycleScope
import com.gyf.immersionbar.ImmersionBar
import com.hjq.language.MultiLanguages
import com.sqoq.downloader3.R
import com.sqoq.downloader3.RepuestTool
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job


open abstract class BaseActivity_ddg<T : ViewDataBinding> : AppCompatActivity() {
    lateinit var binding: T
    private var file4 = "filedsddsa"

    var isFinish = false
   private val request: RepuestTool by lazy {
        RepuestTool(lifecycleScope)
    }
    private var finishListener = mutableListOf<() -> Unit>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initImmersionBar()
        binding = DataBindingUtil.bind(layoutInflater.inflate(layout, null))!!
        setContentView(binding.root)
        initView()
    }

    fun startActivity(mclass: Class<*>, argument: ((intent: Intent) -> Unit)? = null) {
        var intent = Intent(this, mclass)
        argument?.invoke(intent)
        startActivity(intent)
    }

    protected fun <T> request(
        apiCall: suspend CoroutineScope.() -> T,
        onStart: () -> Unit = {},
        onSuccess: (T) -> Unit = {},
        onError: (T) -> Unit = {},
        onComplete: () -> Unit = {},
        onCancel: () -> Unit = {},
        onHttpError: (Throwable?) -> Unit = {},
        onFinally: () -> Unit = {},
        showLoading: Boolean = true,
        showErrorToast: Boolean = true,
        showHttpErrorToast: Boolean = true
    ): Job {
       return request.request(apiCall,onStart,onSuccess, onError, onComplete, onCancel, onHttpError, onFinally, showLoading, showErrorToast, showHttpErrorToast)
    }

    override fun attachBaseContext(newBase: Context?) {
        // 绑定语种
        super.attachBaseContext(MultiLanguages.attach(newBase))
    }

//    var dialog: LoadingDialog? = null
//    fun showWaitDialog(message: String?) {
//        if (dialog == null) {
//            dialog = LoadingDialog(this
//                , message)
//        } else {
//            dialog!!.setMessage(message)
//        }
//        dialog?.show()
//    }
//
//    fun hideWaitDialog() {
//        dialog?.dismiss()
//    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        initImmersionBar()
    }

  open  fun initImmersionBar() {
        ImmersionBar.with(this)
            .navigationBarColor(R.color.white)
            .statusBarDarkFont(true)
            .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED)
            .keyboardEnable(true)
            .flymeOSStatusBarFontColor(android.R.color.black) //修改 flyme OS 状态栏字体颜色
            .init()
    }

    override fun finish() {
        isFinish = true
        finishListener.forEach {
            it.invoke()
        }
        super.finish()
    }


    override fun onDestroy() {
        super.onDestroy()
        finishListener.clear()
    }

    fun addFinishListener(l: () -> Unit) {
        finishListener.add(l)
    }

    fun showAD(
        onShowAdComplete: (() -> Unit)? = null,
        showAd: (() -> Unit)? = null
    ) {

//        AdUtil_fac.showAD(
//            AdTypeData_fac.sc_inter_fac,
//            this@BaseActivity_dac,
//            false,
//            onShowAdComplete_fac = {
//                onShowAdComplete?.invoke()
//            },
//            showAd_fac = {
//                showAd?.invoke()
//            })
    }

    var isShowBack = false
    fun showBackAd() {
        if (enabledShowBackAd()) {
//            if (UiShowUtil_fac.isShowBackAd_fac()) {
//                showAD(onShowAdComplete = { finish() }, showAd = {
//                    isShowBack = true
//                    backAdshow()
//                })
//            } else {
//                finish()
//            }
        } else {
            finish()
        }
    }

    open fun enabledShowBackAd(): Boolean {
        return false
    }

    open fun backAdshow() {

    }

    override fun onBackPressed() {
        isFinish = true
        finishListener.forEach {
            it.invoke()
        }
        super.onBackPressed()
    }

    @get:LayoutRes
    protected abstract val layout: Int
    protected abstract fun initView()
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (enabledShowBackAd() && keyCode == KeyEvent.KEYCODE_BACK) {
            showBackAd()
            return true
        } else {
            return super.onKeyUp(keyCode, event)
        }
    }

}